package com.hrpmar.controller;

import com.hrpmar.controller.dto.DestoryDto;
import com.hrpmar.controller.dto.DrugstockDto;
import com.hrpmar.controller.dto.TakestoDto;
import com.hrpmar.pojo.Druginfo;
import com.hrpmar.pojo.Drugstock;
import com.hrpmar.pojo.Worker;
import com.hrpmar.service.DrugstockService;
import com.hrpmar.utils.Pager;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by 李松松 on 2017/11/20.
 */
@RestController
@RequestMapping("/stock")
public class DrugstockRestController {





    @Resource
    private DrugstockService drugstockService;

    @GetMapping("searchTakessto")
    public Pager searchTakestoAll(TakestoDto dto,Integer offset, Integer limit){
        if(dto.getEndDate()!=null){
            Date dd = dto.getEndDate();
            Calendar calendar = Calendar.getInstance();//获取日历实例
            calendar.setTime(dd);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date d = calendar.getTime();
            dto.setEndDate(d);
        }
        return drugstockService.searchTakestoAll(dto,offset,limit);
    }


    @PostMapping("searchAllWorker")
    public List<Worker> searchAllWorker(){
        return drugstockService.searchAllWorker();
    }

    //修改Map
    @PostMapping("updateSessionDrugstock")
    public String updateSessionDrugstock(String keyId,Drugstock drugstock,HttpServletRequest req){
        String msg = "";
        try {
            HttpSession session = req.getSession();
            Map<String,Drugstock> map =(Map<String,Drugstock>) session.getAttribute("takestoMap");
            Drugstock drugstock1 = map.get(keyId);
            drugstock1.setStoLotnum(drugstock.getStoLotnum());
            drugstock1.setStoPcs(drugstock.getStoPcs());
            msg="修改成功!";
        }catch (Exception ex){
            msg="修改失败!";
        }
        return msg;
    }

    //修改前获取
    @PostMapping("searchSessionByKeyId")
    public Drugstock searchSessionByKeyId(String keyId,HttpServletRequest req){
        HttpSession session = req.getSession();
        Map<String,Drugstock> map =(Map<String,Drugstock>) session.getAttribute("takestoMap");
        Drugstock m = map.get(keyId);
        return m;
    }


    //删除Map1
    @PostMapping("removeDrugstockSession")
    public String removeDrugstockSession(String keyId,HttpServletRequest req){
        HttpSession session = req.getSession();
        Map<String,Drugstock> map =(Map<String,Drugstock>) session.getAttribute("takestoMap");
        String msg = "";
        try {
            map.remove(keyId);
            msg="删除成功!";
        }catch (Exception ex){
            msg="删除失败!";
        }
        return msg;
    }
    //删除session
    @PostMapping("deleteSession")
    public String deleteSession(HttpServletRequest req){
        HttpSession session = req.getSession();
        Map<String,Drugstock> map =(Map<String, Drugstock>) session.getAttribute("takestoMap");
        if(map!=null){
            session.removeAttribute("takestoMap");
        }
        return "";
    }
    //获取session
    @GetMapping("searchSessionMap")
    public Map<String,Drugstock> searchSessionMap(HttpServletRequest req){
        HttpSession session = req.getSession();
        Map<String,Drugstock> map = (Map<String,Drugstock>) session.getAttribute("takestoMap");
        return map;
    }
    @PostMapping("addSessionMap")
    public String addSessionMap(Drugstock drugstock, HttpServletRequest req){
        String keyId = drugstockService.addSessionMap(drugstock,req);
        return keyId;
    }

    @PostMapping("searchDruginfoByNum")
    public Druginfo searchDruginfoByNum(String drugNum){
        return drugstockService.searchByNum(drugNum);
    }

    @PostMapping("searchstoLotnum")
    public List<Drugstock> searchStoLotnum(String drugNum){
        return drugstockService.searchStoLotnum(drugNum);
    }

    @PostMapping("destoryDrugstock")
    public String DestoryDelete(String[] stoNum,String desNum){
        String flag = "false";
        drugstockService.DestoryDelete(stoNum);
        drugstockService.updateDestoryState(desNum);
        flag="true";
        return flag;
    }

    @GetMapping("searchRecord")
    public Pager searchRecord(DestoryDto dto,Integer offset, Integer limit){
        if(dto.getEndDate()!=null){
            Date dd = dto.getEndDate();
            Calendar calendar = Calendar.getInstance();//获取日历实例
            calendar.setTime(dd);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date d = calendar.getTime();
            dto.setEndDate(d);
        }
        Pager pager = drugstockService.searchDestoryRecord(dto,offset,limit);
        return pager;
    }


    @GetMapping("searchOverdue")
    public List<Drugstock> searchOverdueDruginfo(DrugstockDto dto){
        return  drugstockService.searchOverDueDruginfo(dto);
    }




    @PostMapping("searchInsufficientNofen")
    public List<Drugstock> searchInsufficientNoFen(String drugNum){
        return drugstockService.searchKunCunNoFenYe(drugNum);
    }


    @GetMapping("searchInsufficient")
    public Pager searchDrugstockInsufficient(DrugstockDto dto,Integer offset,Integer limit){
        return drugstockService.searchDrugstockinsufficient(dto,offset,limit);
    }


    @PostMapping("update")
    public String update(Drugstock drugstock){
        Integer i = drugstockService.updateDrugstockInfoAspect(drugstock);
        String msg = (i>0?"操作成功！":"操作失败!");
        return msg;
    }


    @GetMapping("/search")
    public Pager searchDrugstock(DrugstockDto dto,Integer offset, Integer limit){
        return drugstockService.searchDrugstock(dto,offset,limit);
    }

    @GetMapping("searchByName")
    public List<Druginfo> searchByName(){
        return drugstockService.searchDrugstockName();
    }

    @GetMapping("searchByDrugNum")
    public Pager searchByDrugNum(DrugstockDto dto,Integer offset,Integer limit){
        if(dto.getStoMfgEnd()!=null){
            Date dd = dto.getStoMfgEnd();
            Calendar calendar = Calendar.getInstance();//获取日历实例
            calendar.setTime(dd);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date d = calendar.getTime();
            dto.setStoMfgEnd(d);
        }
        if(dto.getStoPutindateEnd()!=null){
            Date dd = dto.getStoPutindateEnd();
            Calendar calendar = Calendar.getInstance();//获取日历实例
            calendar.setTime(dd);
            calendar.add(Calendar.DAY_OF_MONTH,1);
            Date d = calendar.getTime();
            dto.setStoPutindateEnd(d);
        }
        return drugstockService.searchDetailBydrunNum(dto,offset,limit);
    }
}
